package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.FoundGoodTitleBeen;
import cn.xfdzx.android.apps.shop.bean.HomeRetailBeen;
import cn.xfdzx.android.apps.shop.bean.NearShopBeen;
import cn.xfdzx.android.apps.shop.contract.HomeNearShopContract;
import cn.xfdzx.android.apps.shop.contract.HomeRetailContract;
import cn.xfdzx.android.apps.shop.model.HomeNearShopModel;
import cn.xfdzx.android.apps.shop.model.HomeRetailModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author
 * @date Description：
 */
public class HomeNearShopPresenter extends BasePresenter<HomeNearShopContract.View> implements HomeNearShopContract.Presenter {

    private HomeNearShopContract.Model model;

    public HomeNearShopPresenter() {
        model = new HomeNearShopModel();
    }


    @Override
    public void initNearData(boolean isRefresh, boolean isLoading,String lng, String lat, String distance, String sort, String page, String limit) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.initNearData(lng,  lat,  distance,  sort,  page,  limit).compose(RxScheduler.<NearShopBeen>Flo_io_main())
                .as(mView.<NearShopBeen>bindAutoDispose()).subscribe(new Consumer<NearShopBeen>() {
            @Override
            public void accept(NearShopBeen baseObjectBean) throws Exception {
                mView.onInitNearDataSuccess(baseObjectBean);
                if (isLoading){
                    mView.hideLoading();
                }
                if (isRefresh){
                    mView.finishRefresh();
                }else {
                    mView.finishLoadMore();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onError(throwable);
                if (isLoading){
                    mView.hideLoading();
                }
                if (isRefresh){
                    mView.finishRefresh();
                }else {
                    mView.finishLoadMore();
                }
            }
        });
    }
}
